<?php
class JobeditCtl extends BaseCtl {
	public function JobeditCtl(){
		parent::BaseCtl();
	}
	
    public function isPostback(){
    	return 'jobedit' == $this->getPost('pagename');
    }
    
    public function loadData(){
    	$form = $this->getJobeditForm();
    	
    	$form->setId($this->getPost('id'));
    	$form->setName($this->getPost('name'));
    	$form->setStatus($this->getPost('status'));
    	$form->setAdding('yes' == $this->getPost('adding'));
    }
		
	public function save(){
		try {
			// get the user id from the session
			$userId = HtmlUtil::getSessionUser()->getUser()->getId();
			$form = $this->getJobeditForm();
			
			// we assume the id is passed from the joblist when trying to secure an add
			$job = new Job();
			$job->setId($form->getId());
			$job->setName($form->getName());
			$job->setStatus($form->getStatus());
			$job->setUserId($userId);
			
			$service = new Service();
			$listform = $this->getJoblistForm();
			
			if($form->isAdding()){
				$service->addJob($job);
				$listform->addJob($job);
			}
			else {
				$service->updateJob($job);
				$listform->updateJob($job);
			}
			
			$this->redirect('joblist.php');
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
	
	public function cancel(){
		try {
			$this->redirect('joblist.php');
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
} 
?>